package com.apache.portal.weixin.core.manager.impl;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;

import com.apache.portal.weixin.core.manager.WxRecvMsgParser;
import com.apache.portal.weixin.entity.recv.WxRecvMsg;

/**
  * description: 消息转换代理接口提抽像类
  * @author Hou DaYu 创建时间：下午05:33:44
 */
public abstract class WxRecvMsgBaseParser implements WxRecvMsgParser {

	public WxRecvMsg parser(Document doc) throws JDOMException {

		Element root = doc.getRootElement();
		String toUserName = getElementText(root, "ToUserName");
		String fromUserName = getElementText(root, "FromUserName");
		String createTime = getElementText(root, "CreateTime");
		String msgType = getElementText(root, "MsgType");
		String msgId = getElementText(root, "MsgId");

		return parser(root, new WxRecvMsg(toUserName, fromUserName, createTime, msgType, msgId));
	}

	protected abstract WxRecvMsg parser(Element root, WxRecvMsg msg) throws JDOMException;

	protected String getElementText(Element elem, String xpath) throws JDOMException {
		return elem.getChildText(xpath);
	}

}
